package com.quick.im.client.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quick.im.client.interfaces.IMessageRecordService;
import com.quick.im.client.mapper.MessageRecordMapper;
import com.quick.im.common.entity.domian.MessageRecord;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
* 一对一消息表(MessageToOne) 服务实现类
* @author xhh
* @since 2024-12-12
*/
@Service
@Slf4j
public class MessageRecordServiceImpl extends ServiceImpl<MessageRecordMapper, MessageRecord> implements IMessageRecordService {


    @Setter(onMethod_ = @Autowired)
    private MessageRecordMapper dataMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MessageRecord saveMessageToOne (MessageRecord request) {
        save(request);
        return request;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteMessageToOneById (Integer id) {
        removeById(id);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MessageRecord updateMessageToOne (MessageRecord request) throws IllegalAccessException, NoSuchMethodException {

        updateById(request);
        return request;
    }

    @Override
    public  Page<MessageRecord> listMessageToOne (MessageRecord request) throws IllegalAccessException, NoSuchMethodException {
        LambdaQueryWrapper<MessageRecord> query = Wrappers.lambdaQuery();
        Page<MessageRecord> page = new Page<>(request.getPage(),request.getPageSize());
        Page<MessageRecord> res = page(page, query);
        return res;
    }

    @Override
    public MessageRecord getMessageToOneById (Integer id){
        Optional<MessageRecord> data = Optional.ofNullable(getById(id));
        return data.orElse(null);
    }

}

